const WebpackBar = require('webpackbar');
const path = require('path');
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
const ESLintPlugin = require('eslint-webpack-plugin');
const CircularDependencyPlugin = require('circular-dependency-plugin');
const WebpackBuildNotifierPlugin = require('webpack-build-notifier');
const JavaScriptObfuscator = require('webpack-obfuscator');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const StyleLintPlugin = require('stylelint-webpack-plugin');
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
const webpack = require('webpack');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
require('dotenv').config({ path: '.env.development' });
// 此处可以看到，在不同的webpackconfig文件中使用dotenv提供的api配置对应的env文件，env文件里的环境变量就会被写入process.env对象中，通过指定不同的env文件来区分环境变量，不用dotenv插件也可以手动在webpack.config文件中以对象属性赋值的方式来添加环境变量,例如:
/* 
process.env.REACT_APP_BASE_URL = "http://www.baidu.com"

*/
const env = process.env;
// webpack不缺理论知识，更缺实践经验
module.exports = {
  mode: 'development', // mode有两种，production/development,采用不同的mode，webpack会使用不同的的默认配置，
  /* 
  production : 生产模式，开启了代码混淆，代码压缩，
  development: 开发模式，不开启代码混淆，不压缩代码
  */
  // __dirname可以理解为./
  entry: path.resolve(__dirname, '../src/index.js'),

  output: {
    path: path.resolve(__dirname, '../dist'),

    filename: 'js/bundle.js',
  },
  /*  */
  plugins: [
    new CompressionPlugin(),
    new CaseSensitivePathsPlugin(),
    // suppressSuccess: true 设置只在第一次编译成功时输出成功的通知, rebuild 成功的时候不通知
    new WebpackBuildNotifierPlugin({ suppressSuccess: true }),
    new WebpackBar({
      name: 'react-typescript-boilerplate',
      // react 蓝
      color: '#6b61fb',
    }),
    new ESLintPlugin({
      // Plugin options
      extensions: ['js', 'mjs', 'jsx', 'ts', 'tsx'],
      // failOnError: false, // 重点这个配置
      // context: paths.appSrc,
      cache: true,
      // ESLint class options
      // cwd: paths.appPath,
      resolvePluginsRelativeTo: __dirname,
      baseConfig: {
        rules: {
          'react/react-in-jsx-scope': 'error',
        },
      },
    }),
    new StyleLintPlugin({
      // 正则匹配想要lint监测的文件
      files: ['src/*.css', 'src/**/*.scss', 'src/assets/styles/*.l?(e|c)ss'],
    }),
    new FriendlyErrorsWebpackPlugin({
      compilationSuccessInfo: {
        messages: [`Your application is running here: http://localhost:3000`],
      },
      onErrors: undefined,
      clearConsole: true,
    }),
    new HtmlWebpackPlugin({
      // chunks:['test'], //html文件需要
      template: './public/index.html', // 模板文件，即需要打包和拷贝到build目录下的html文件
      filename: 'index.html', // 目标html文件
      inject: 'body', // 资源加入到底部，把模块引入到html文件的底部
      title: '测试版本',
    }),
    // new CleanWebpackPlugin(),
    // 这个插件允许在编译时将代码中的变量替换为其他值或表达式
    new webpack.DefinePlugin({
      'process.env': JSON.stringify(process.env),
    }),
    new ForkTsCheckerWebpackPlugin({}),
  ],
  optimization: {
    // minimize: false,
    // minimizer: [new UglifyJSPlugin()],
    nodeEnv: 'development',
  },
  module: {
    rules: [
      {
        test: /\.(js|mjs|jsx|ts|tsx)$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        options: {
          presets: ['@babel/preset-env', '@babel/preset-react'],
        },
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader'],
      },
    ],
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, '../src'),
    },
    extensions: ['.tsx', '.js', '.ts'],
  },
  devServer: {
    // 项目构建后的路径
    // contentBase: path.resolve(__dirname,'dist'),
    // 启动gzip压缩
    // https: true,
    // client: {
    //   progress: true,
    // },
    static: {
      directory: path.join(__dirname, '../public'),
      staticOptions: {
        redirect: true,
      },
    },
    compress: false,
    // 端口号
    port: 3001,
    // 自动打开浏览器
    open: true,
  },
  performance: false,
  stats: 'errors-only',
  devtool: 'source-map',
};

// webpack 基础部分
// 1、起dev-server
// 2、设置开发/生产环境webpack.config配置文件/env环境变量文件
// 3、配置source-map
// 4、看官网的其他配置都加进去、用起来
// 5、尝试去配出一个react + ts项目

// webpack  进阶部分
// 1、原理
// 2、性能优化
